package com.yunzhou.service.impl;

import com.yunzhou.entity.ForumBoard;
import com.yunzhou.mapper.ForumBoardMapper;
import com.yunzhou.service.ForumBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ForumBoardServiceImpl implements ForumBoardService {

    @Autowired
    private ForumBoardMapper forumBoardMapper;

    // 获取一级板块以及对应的二级板块
    @Override
    public List<ForumBoard> getForumBoard() {
        // 1、先获取所有的一级板块
        List<ForumBoard> forumBoard1 = forumBoardMapper.getForumBoard1();

        // 2、将所有的一级板块ID作为参数传入，查询所有满足条件( in )的二级板块
        List<Integer> parentBoardIds = forumBoard1.stream().map(ForumBoard::getBoardId).distinct().collect(Collectors.toList());
        List<ForumBoard> forumBoard2 = forumBoardMapper.getForumBoard2ByParentBoardIds(parentBoardIds);

        // 3、将一级板块对应的二级板块插入到一级板块的children中
        Map<Integer, List<ForumBoard>> tempMap = forumBoard2.stream().collect(Collectors.groupingBy(ForumBoard::getParentBoardId));
        forumBoard1.forEach(item -> {
            item.setChildren(tempMap.get(item.getBoardId()));
        });

        return forumBoard1;
    }
}
